﻿using AutoMapper;
using Blm.Utils.Extensions;
using Language.Resource;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Localization;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using VisionCloud.Core;
using VisionCloud.Core.Model.Sys;
using VisionCloud.Domain.Common;
using VisionCloud.Domain.DtoModel.Sys;
using VisionCloud.Domain.Extension;
using VisionCloud.Domain.Repository.Interface.Sys;
using VisionCloud.Utility.Extensions;

namespace VisionCloud.Domain.Repository.Implement.Sys
{
    public class SysDeviceRepository : BaseRepository<SysDevice>, ISysDeviceRepository
    {
        public IStringLocalizer<RepositoryResource> Localizer { get; set; }

        public IMapper Mapper { set; get; }

        public class DeviceAssignData
        {
            public int AssignedCount { get; set; }
            public int NoAssignCount { get; set; }
        }

        /// <summary>
        /// 获取没有进行逻辑删除的列表，含分页参数
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public async Task<ApiPageResult> GetSysDeviceNoDelete(SearcheParaSysDevicePage condition)
        {
            var token = OperatorProvider.Get();
            condition.CompanyGuid = OperatorProvider.Get().CompanyGuid;
            //是否激活搜索条件
            bool isHaveActive = !string.IsNullOrEmpty(condition.IsActive);
            bool isActive = (condition.IsActive == "已激活" ? true : false);

            var query = (from t in Db.SysDevices
                         where (token != null && (token.IsCompanyAdmin || t.DeviceUserId == token.YunUserGuid)) //加入过滤条件
                         && t.IsDeleted == false
                         && (condition.CompanyGuid == Guid.Empty || t.SysOrgId == condition.CompanyGuid.ToString())
                         && (condition.DeviceGuid == Guid.Empty || t.DeviceGuid == condition.DeviceGuid)
                         && (isHaveActive ? (t.IsActive == isActive) : true)
                         &&
                         (string.IsNullOrEmpty(condition.dogNO) ? true : t.EncryptedDogNumber.Contains(condition.dogNO))
                         &&
                         (condition.DeviceUserId == null || (t.DeviceUserId == null || t.DeviceUserId == Guid.Empty || (t.DeviceUserId == condition.DeviceUserId)))  // 获取当前用户的设备+未分配的设备
                         &&
                         (string.IsNullOrEmpty(condition.CustomerCompanyName) ? true : t.CustomerCompanyName.Contains(condition.CustomerCompanyName))
                         &&
                         (string.IsNullOrEmpty(condition.DeviceNumber) ? true : t.DeviceNumber.Contains(condition.DeviceNumber))
                         select t
              ).WhereIf(
                   !condition.KeyWord.IsEmpty(),
                   w => w.DeviceNumber.Contains(condition.KeyWord) || w.DeviceUserName.Contains(condition.KeyWord)
                   || (w.IsActive ? "已激活" : "未激活").Contains(condition.KeyWord)
                   || w.DeviceSoftwareVersion.Contains(condition.KeyWord)
                 )
              .WhereIf(
                   !condition.DeviceDateOfProduction_begin.IsNullOrWhiteSpace()
                 && condition.DeviceDateOfProduction_begin.ToDateTime().HasValue, w => w.DeviceDateOfProduction >= condition.DeviceDateOfProduction_begin.ToDateTime()
                 )
              .WhereIf(
                    !condition.DeviceDateOfProduction_End.IsNullOrWhiteSpace()
                   && condition.DeviceDateOfProduction_End.ToDateTime().HasValue, w => w.DeviceDateOfProduction <= condition.DeviceDateOfProduction_End.ToDateTime()
                 )
               .AsNoTracking();

            List<SysDevice> curlist = null;
            if (!string.IsNullOrEmpty(condition.OrderBy))
            {
                // 底层按照IsActive排序
                if (string.Equals(condition.OrderBy, "IsActiveStr", StringComparison.OrdinalIgnoreCase))
                {
                    condition.OrderBy = "IsActive";
                }

                // 先根据用户选中排序，然后再根据创建时间降序排序
                curlist = query.PageOrderBy(condition).Skip((condition.CurrentPage - 1) * condition.PageSize).Take(condition.PageSize).ToList();
            }
            else
            {
                // 默认按照设备创建时间降序排序
                condition.OrderBy = "CreatedDate";
                condition.AscDesc = "DESC";
                curlist = query.PageOrderBy(condition).Skip((condition.CurrentPage - 1) * condition.PageSize).Take(condition.PageSize).ToList();
            }

            var sysDeviceDtos = Mapper.Map<List<SysDevice>, List<SysDeviceDto>>(curlist);
            var result = condition.CopyTo<ApiPageResult>();
            result.CurrentPage = condition.CurrentPage;
            result.Total = query.Count();
            result.PageData = sysDeviceDtos;
            var assignData = new DeviceAssignData
            {
                AssignedCount = query.Count(p => p.DeviceUserId != Guid.Empty)
            };
            assignData.NoAssignCount = query.Count() - assignData.AssignedCount;
            result.Data = assignData;
            return result;
        }

        /// <summary>
        /// 获取设备信息
        /// </summary>
        /// <param name="query">查询条件</param>
        /// <returns></returns>
        public async Task<SysDevice> GetModel(DeviceCondtion query)
        {
            var result = await (from t in Db.SysDevices
                                where !t.IsDeleted 
                                && (query.DeviceGuid == Guid.Empty || t.DeviceGuid == query.DeviceGuid)
                                && (string.IsNullOrWhiteSpace(query.ScannerNumber) || t.ScannerNumber == query.ScannerNumber)
                                && (string.IsNullOrWhiteSpace(query.ScannerType) || t.ScannerType == query.ScannerType)
                                select t
                           ).AsNoTracking().FirstOrDefaultAsync().ConfigureAwait(false);
            return result;
        }

        private bool CheckModel(SysDevice device)
        {
            if (device == null)
            {
                throw new OperationException(Localizer["YB0231"].Value);
            }
            else
            {
                if (string.IsNullOrEmpty(device.DeviceNumber))
                {
                    throw new OperationException(Localizer["YB0232"].Value);
                }

                if (string.IsNullOrEmpty(device.CustomerCompanyName))
                {
                    throw new OperationException(Localizer["YB0233"].Value);
                }

                if (device.DeviceDateOfProduction == null)
                {
                    throw new OperationException(Localizer["YB0234"].Value);
                }

                if (string.IsNullOrEmpty(device.CustomerPhoneNumber))
                {
                    throw new OperationException(Localizer["YB0235"].Value);
                }

                if (device.DeviceExpire == null)
                {
                    throw new OperationException(Localizer["YB0236"].Value);
                }

                if (device.ScannerExpire == null)
                {
                    throw new OperationException(Localizer["YB0237"].Value);
                }

                if (device.ComputerExpire == null)
                {
                    throw new OperationException(Localizer["YB0238"].Value);
                }

                if (device.TripodExpire == null)
                {
                    throw new OperationException(Localizer["YB0239"].Value);
                }

                if (string.IsNullOrEmpty(device.TripodType))
                {
                    throw new OperationException(Localizer["YB0240"].Value);
                }

                if (string.IsNullOrEmpty(device.ComputerNumber))
                {
                    throw new OperationException(Localizer["YB0241"].Value);
                }

                if (string.IsNullOrEmpty(device.ComputerType))
                {
                    throw new OperationException(Localizer["YB0242"].Value);
                }

                if (string.IsNullOrEmpty(device.ScannerNumber))
                {
                    throw new OperationException(Localizer["YB0243"].Value);
                }

                if (string.IsNullOrEmpty(device.ScannerType))
                {
                    throw new OperationException(Localizer["YB0244"].Value);
                }

                if (string.IsNullOrEmpty(device.EncryptedDogNumber))
                {
                    throw new OperationException(Localizer["YB0245"].Value);
                }

                if (string.IsNullOrEmpty(device.EncryptedDogType))
                {
                    throw new OperationException(Localizer["YB0246"].Value);
                }
            }
            return true;
        }

        private bool CheckAssignModel(AssignSysDeviceDto device)
        {
            if (device == null)
            {
                throw new OperationException(Localizer["YB0231"].Value);
            }
            else
            {
                if (string.IsNullOrEmpty(device.DeviceUserId))
                {
                    throw new OperationException(Localizer["YB0247"].Value);
                }

                if (string.IsNullOrEmpty(device.DeviceUserName))
                {
                    throw new OperationException(Localizer["YB0248"].Value);
                }
            }
            return true;
        }

        public async Task<ApiResult> UpdateDevice(SysDevice device)
        {
            CheckModel(device);

            if (device != null)
            {
                if (device.DeviceGuid == null || device.DeviceGuid == Guid.Empty)
                {
                    return new ApiResult() { Code = Common.AjaxCode.Error, Message = Localizer["YB0249"].Value };
                }

                var IsExisted = await this.GetOtherExistedModel(device);
                if (IsExisted != null)
                {
                    if (string.Equals(IsExisted.DeviceNumber, device.DeviceNumber))
                    {
                        return new ApiResult() { Code = Common.AjaxCode.Error, Message = Localizer["YB0250"].Value };
                    }
                    if (string.Equals(IsExisted.EncryptedDogNumber, device.EncryptedDogNumber) && device.EncryptedDogType == IsExisted.EncryptedDogType)
                    {
                        return new ApiResult() { Code = Common.AjaxCode.Error, Message = Localizer["YB0251"].Value };
                    }
                    if (string.Equals(IsExisted.ComputerNumber, device.ComputerNumber) && device.ComputerType == IsExisted.ComputerType)
                    {
                        return new ApiResult() { Code = Common.AjaxCode.Error, Message = Localizer["YB0252"].Value };
                    }
                    if (string.Equals(IsExisted.ScannerNumber, device.ScannerNumber) && device.ScannerType == IsExisted.ScannerType)
                    {
                        return new ApiResult() { Code = Common.AjaxCode.Error, Message = Localizer["YB0253"].Value };
                    }
                    return new ApiResult() { Code = Common.AjaxCode.Error, Message = Localizer["YB0254"].Value };
                }

                var item = await this.GetByIdAsync(device.DeviceGuid);
                if (item == null)
                {
                    return new ApiResult() { Code = Common.AjaxCode.Error, Message = Localizer["YB0255"].Value };
                }
                else
                {
                    item.ComputerDateOfProduction = device.ComputerDateOfProduction;
                    item.ComputerExpire = device.ComputerExpire;
                    item.ComputerNumber = device.ComputerNumber;
                    item.ComputerType = device.ComputerType;
                    item.ComputerSystemVersion = device.ComputerSystemVersion;
                    item.CustomerCompanyName = device.CustomerCompanyName;
                    item.DeviceExpire = device.DeviceExpire;
                    item.DeviceNumber = device.DeviceNumber;
                    item.DeviceSoftwareVersion = device.DeviceSoftwareVersion;
                    item.EncryptedDogType = device.EncryptedDogType;
                    item.EncryptedDogNumber = device.EncryptedDogNumber;
                    item.LastUpdatedBy = OperatorProvider.Get()?.YunUserGuid ?? Guid.Empty;
                    item.LastUpdatedDate = DateTime.Now;
                    item.ScannerDateOfProduction = device.ScannerDateOfProduction;
                    item.ScannerExpire = device.ScannerExpire;
                    item.ScannerFirmwareVersion = device.ScannerFirmwareVersion;
                    item.ScannerNumber = device.ScannerNumber;
                    item.ScannerType = device.ScannerType;
                    item.DeviceDateOfProduction = device.DeviceDateOfProduction;
                    item.TripodExpire = device.TripodExpire;
                    item.TripodType = device.TripodType;
                    item.CustomerPhoneNumber = device.CustomerPhoneNumber;
                    await Db.SaveChangesAsync();
                    return new ApiResult() { Code = Common.AjaxCode.Success, Result = item };
                }
            }
            else
            {
                return new ApiResult() { Code = Common.AjaxCode.Error, Message = Localizer["YB0256"].Value };
            }
        }

        public async Task<ApiResult> AddDevice(SysDevice device)
        {
            CheckModel(device);
            if (device != null)
            {
                var IsExisted = await this.GetExistedModel(device);
                if (IsExisted != null)
                {
                    if (string.Equals(IsExisted.DeviceNumber, device.DeviceNumber))
                    {
                        return new ApiResult() { Code = Common.AjaxCode.Error, Message = Localizer["YB0250"].Value };
                    }
                    if (string.Equals(IsExisted.EncryptedDogNumber, device.EncryptedDogNumber) && device.EncryptedDogType == IsExisted.EncryptedDogType)
                    {
                        return new ApiResult() { Code = Common.AjaxCode.Error, Message = Localizer["YB0251"].Value };
                    }
                    if (string.Equals(IsExisted.ComputerNumber, device.ComputerNumber) && device.ComputerType == IsExisted.ComputerType)
                    {
                        return new ApiResult() { Code = Common.AjaxCode.Error, Message = Localizer["YB0252"].Value };
                    }
                    if (string.Equals(IsExisted.ScannerNumber, device.ScannerNumber) && device.ScannerType == IsExisted.ScannerType)
                    {
                        return new ApiResult() { Code = Common.AjaxCode.Error, Message = Localizer["YB0253"].Value };
                    }
                    return new ApiResult() { Code = Common.AjaxCode.Error, Message = Localizer["YB0254"].Value };
                }
                else
                {
                    device.DeviceGuid = Guid.NewGuid();
                    device.CreatedBy = OperatorProvider.Get()?.YunUserGuid ?? Guid.Empty;
                    device.CreatedDate = DateTime.Now;
                    device.LastUpdatedBy = OperatorProvider.Get()?.YunUserGuid ?? Guid.Empty;
                    device.CreatedDate = DateTime.Now;
                    Db.SysDevices.Add(device);
                    await Db.SaveChangesAsync();
                    return new ApiResult() { Code = Common.AjaxCode.Success, Result = device };
                }
            }
            else
            {
                return new ApiResult() { Code = Common.AjaxCode.Error, Message = Localizer["YB0256"].Value };
            }
        }

        public async Task<ApiResult> AssignDeviceUser(AssignSysDeviceDto device)
        {
            CheckAssignModel(device);
            if (device != null)
            {
                var user = Db.SysUsers.Single(u => u.YunUserGuid == device.DeviceUserId.ToGuid());
                if (user == null || user.Name != device.DeviceUserName)
                    return new ApiResult() { Code = Common.AjaxCode.Error, Message = Localizer["YB0257"].Value };
                foreach (var deviceGuid in device.DeviceGuids)
                {
                    var item = await this.GetByIdAsync(deviceGuid.ToGuid());
                    if (item == null)
                    {
                        return new ApiResult() { Code = Common.AjaxCode.Error, Message = Localizer["YB0255"].Value };
                    }
                    else
                    {
                        if (item.SysOrgId != device.CurUserOrgId)
                        {
                            return new ApiResult() { Code = Common.AjaxCode.Error, Message = Localizer["W22040601"].Value };
                        }

                        item.LastUpdatedBy = OperatorProvider.Get()?.YunUserGuid ?? Guid.Empty;
                        item.LastUpdatedDate = DateTime.Now;
                        item.DeviceUserId = device.DeviceUserId.ToGuid();
                        item.DeviceUserName = device.DeviceUserName;
                    }
                }
                await Db.SaveChangesAsync();
                return new ApiResult() { Code = Common.AjaxCode.Success };
            }
            else
            {
                return new ApiResult() { Code = Common.AjaxCode.Error, Message = Localizer["YB0256"].Value };
            }
        }

        /// <summary>
        /// 删除设备管理用户
        /// </summary>
        /// <param name="device">设备信息</param>
        /// <returns></returns>
        public async Task<ApiResult> DeleteDeviceUser(AssignSysDeviceDto device)
        {
            if (device != null)
            {
                foreach (var deviceGuid in device.DeviceGuids)
                {
                    var item = await this.GetByIdAsync(deviceGuid.ToGuid());
                    if (item == null)
                    {
                        return new ApiResult() { Code = Common.AjaxCode.Error, Message = Localizer["YB0255"].Value };
                    }
                    else
                    {
                        item.LastUpdatedBy = OperatorProvider.Get()?.YunUserGuid ?? Guid.Empty;
                        item.LastUpdatedDate = DateTime.Now;
                        item.DeviceUserId = Guid.Empty;
                        item.DeviceUserName = null;
                    }
                }
                await Db.SaveChangesAsync();
                return new ApiResult() { Code = Common.AjaxCode.Success };
            }
            else
            {
                return new ApiResult() { Code = Common.AjaxCode.Error, Message = Localizer["YB0256"].Value };
            }
        }

        public async Task<ApiResult> DeletesAsync(Guid[] deviceIds)
        {
            if (deviceIds == null || deviceIds.Length == 0)
            {
                return new ApiResult() { Code = Common.AjaxCode.Error, Message = Localizer["YB0258"].Value };
            }
            var result = await BlueDbContext.ExecuteAsync("update \"SysDevice\" set \"IsDeleted\"=true WHERE  \"DeviceGuid\" in (" + deviceIds.GetArrayStrForSql(",") + ");");
            if (result > 0)
                return new ApiResult() { Code = Common.AjaxCode.Success };
            else
                return new ApiResult() { Code = Common.AjaxCode.Error, Message = Localizer["YB0259"].Value };
        }

        /// <summary>
        /// 是否存在重复编号的设备
        /// </summary>
        /// <param name="SysDeviceDto"></param>
        /// <returns></returns>
        public async Task<bool> IsExisted(SysDevice SysDeviceDto)
        {
            return await (from t in Db.SysDevices
                          select t)
                                 .AnyAsync(i =>
                                 (i.IsDeleted == false)
                                 &&
                                 (i.EncryptedDogType == SysDeviceDto.EncryptedDogType && i.EncryptedDogNumber == SysDeviceDto.EncryptedDogNumber)
                                 ||
                                 (i.DeviceNumber == SysDeviceDto.DeviceNumber)
                                 ||
                                 (i.ComputerNumber == SysDeviceDto.ComputerNumber && i.ComputerType == SysDeviceDto.ComputerType)
                                 ||
                                 (i.ScannerNumber == SysDeviceDto.ScannerNumber && i.ScannerType == SysDeviceDto.ScannerType)
                                 );
        }

        public async Task<SysDevice> GetExistedModel(SysDevice SysDeviceDto)
        {
            return await (from t in Db.SysDevices
                          where t.IsDeleted == false
                                &&
                                ((t.EncryptedDogType == SysDeviceDto.EncryptedDogType && t.EncryptedDogNumber == SysDeviceDto.EncryptedDogNumber) ||
                                t.DeviceNumber == SysDeviceDto.DeviceNumber ||
                                (t.ComputerNumber == SysDeviceDto.ComputerNumber && t.ComputerType == SysDeviceDto.ComputerType) ||
                                (t.ScannerNumber == SysDeviceDto.ScannerNumber && t.ScannerType == SysDeviceDto.ScannerType))
                          select t
                ).FirstOrDefaultAsync();
        }

        /// <summary>
        /// 判断更新内容是否已存在
        /// </summary>
        /// <param name="SysDeviceDto"></param>
        /// <returns></returns>
        public async Task<SysDevice> GetOtherExistedModel(SysDevice SysDeviceDto)
        {
            return await (from t in Db.SysDevices
                          where t.IsDeleted == false &&
                                t.DeviceGuid != SysDeviceDto.DeviceGuid
                                && (
                                (t.EncryptedDogType == SysDeviceDto.EncryptedDogType && t.EncryptedDogNumber == SysDeviceDto.EncryptedDogNumber) ||
                                t.DeviceNumber == SysDeviceDto.DeviceNumber ||
                                (t.ComputerNumber == SysDeviceDto.ComputerNumber && t.ComputerType == SysDeviceDto.ComputerType) ||
                                (t.ScannerNumber == SysDeviceDto.ScannerNumber && t.ScannerType == SysDeviceDto.ScannerType))
                          select t
                ).FirstOrDefaultAsync();
        }

        public async Task<ApiResult> GetDevice(SysDevice sysDeviceDto)
        {
            string msg = string.Empty;
            var temp = await (from t in Db.SysDevices
                              where t.IsDeleted == false
                              select t)
                                 .FirstOrDefaultAsync(i =>
                                 (i.EncryptedDogType == sysDeviceDto.EncryptedDogType && i.EncryptedDogNumber == sysDeviceDto.EncryptedDogNumber)
                                 ||
                                 (i.DeviceNumber == sysDeviceDto.DeviceNumber)
                                 ||
                                 (i.ComputerNumber == sysDeviceDto.ComputerNumber && i.ComputerType == sysDeviceDto.ComputerType)
                                 ||
                                 (i.ScannerNumber == sysDeviceDto.ScannerNumber && i.ScannerType == sysDeviceDto.ScannerType)
                                );

            if (temp == null || temp.IsDeleted == true)
            {
                return new ApiResult() { Code = Common.AjaxCode.Success, Result = null, Message = msg };
            }

            string wordWrap = "\r\n";
            msg += string.Format(Localizer["YB0260"].Value, sysDeviceDto.RowIndex) + wordWrap;
            //msg += $"第{sysDeviceDto.RowIndex}行数据与数据库中的重复:" + wordWrap;

            if (temp.EncryptedDogType == sysDeviceDto.EncryptedDogType && temp.EncryptedDogNumber == sysDeviceDto.EncryptedDogNumber)
            {
                msg += string.Format(Localizer["YB0261"].Value, temp.EncryptedDogNumber) + wordWrap;
            }

            if (temp.DeviceNumber == sysDeviceDto.DeviceNumber)
            {
                msg += string.Format(Localizer["YB0262"].Value, temp.DeviceNumber) + wordWrap;
                //msg += $"产品序列号{ temp.DeviceNumber}已存在;" + wordWrap;
            }

            if (temp.ComputerNumber == sysDeviceDto.ComputerNumber)
            {
                msg += string.Format(Localizer["YB0263"].Value, temp.ComputerNumber) + wordWrap;
                //msg += $"计算机编号{temp.ComputerNumber}已存在;" + wordWrap;
            }

            if (temp.ScannerNumber == sysDeviceDto.ScannerNumber)
            {
                msg += string.Format(Localizer["YB0264"].Value, temp.ScannerNumber) + wordWrap;
                //msg += $"扫描仪编号{temp.ScannerNumber }已存在;" + wordWrap;
            }

            return new ApiResult() { Code = Common.AjaxCode.Success, Result = temp, Message = msg };
        }

        /// <summary>
        /// 是否存在重复编号的设备
        /// </summary>
        /// <param name="SysDeviceDto"></param>
        /// <returns></returns>
        public async Task<bool> IsExisted(SysDeviceDto SysDeviceDto)
        {
            return await (from t in Db.SysDevices
                          select t)
                                 .AnyAsync(i =>
                                 (i.IsDeleted == false)
                                 &&
                                 (i.EncryptedDogType == SysDeviceDto.EncryptedDogType && i.EncryptedDogNumber == SysDeviceDto.EncryptedDogNumber)
                                 ||
                                 (i.DeviceNumber == SysDeviceDto.DeviceNumber)
                                 ||
                                 (i.ComputerNumber == SysDeviceDto.ComputerNumber && i.ComputerType == SysDeviceDto.ComputerType)
                                 ||
                                 (i.ScannerNumber == SysDeviceDto.ScannerNumber && i.ScannerType == SysDeviceDto.ScannerType)
                                 );
        }

        /// <summary>
        /// 作者：黄钧航
        /// 根据用户Id获取该用户设备
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public async Task<List<SysDevice>> GetDevicesByUserId(SysDeviceDto condition)
        {
            return await (
                from h in Db.SysDevices
                where (!h.IsDeleted && h.DeviceUserId == condition.DeviceUserId)
                select h).AsNoTracking().ToListAsync().ConfigureAwait(false);
        }

        /// <summary>
        /// 作者：黄钧航
        /// 获取所有已激活设备
        /// </summary>
        /// <returns></returns>
        public async Task<List<SysDeviceDtoPart>> GetAllActivedDevices()
        {
            return await (
                from h in Db.SysDevices
                where (!h.IsDeleted && h.IsActive && h.CustomerCompanyName != "暂未分配")
                orderby h.CreatedDate descending
                select new SysDeviceDtoPart()
                {
                    DeviceGuid = h.DeviceGuid,
                    DeviceSoftwareVersion = h.DeviceSoftwareVersion,
                    DeviceNumber = h.DeviceNumber,
                    CreatedDate = h.CreatedDate,
                }).AsNoTracking().ToListAsync().ConfigureAwait(false);
        }

        public async Task<bool> UpdateDeviceVersion(SysDeviceVersionDto versionDto)
        {
            var device = await Db.SysDevices.FirstOrDefaultAsync(x => x.EncryptedDogNumber == versionDto.UsbKeyNo);
            if (device == null)
            {
                //Logger.Info($"更新加密狗未查询到设备:{versionDto.UsbKeyNo}");
                return true;
            }
            device.DeviceSoftwareVersion = versionDto.SoftVersion;
            device.DeviceMac = versionDto.DeviceMac;
            device.LastUpdatedDate = DateTime.Now;
            return await Db.SaveChangesAsync() > 0;
        }
    }
}